<?php

	fta_registerModules(
		array(
			"FTAORM/FTAORMFullCmsContentMigrator",
		)
	);

	class FTAPageBuilderMigratorBase extends FTAORMFullCmsContentMigrator {

		
		// migrate
		public function migrate($newZdb){
				
				// load everything..
				$this->fault();
			
				//flip the database			
				$this->zdb = $newZdb;
				$this->cmscontentmetadata->zdb = $newZdb;
				
								
				//now.. check for an existing version or quietly yank? and always save new..
				$pk = $this->getPrimaryKey();
				$this->yankForDocumentCode($this->getPrimaryKey());
			
				
				
				//yank the primary keys
				$this->setPrimaryKey(null);
				$this->cmscontentmetadata->setPrimaryKey(null);						
				
				if (!$this->save($pk)) {
					throw new Exception('Error saving / migrating record');
				}
				
				
				//now the binarydata, we cannot rely on the save mechanisim in the orm to 
				// handle the relationship correctly.
				
				foreach ($this->binarydata as $bd){
					$bd->zdb = $newZdb;
					$pk = $bd->getPrimaryKey();
					$bd->setPrimaryKey(null);
					$bd->save($pk);// do we care about failures here??
				}
				
				foreach ($this->manytomanydata as $md){
					$md->zdb = $newZdb;
					$pk = $md->getPrimaryKey();
					$md->setPrimaryKey(null);
					$md->save($pk);// do we care about failures here??
				}				

				foreach ($this->multiplekeyvaluepairs as $mk){
					$mk->zdb = $newZdb;
					$pk = $mk->getPrimaryKey();
					$mk->setPrimaryKey(null);
					$mk->save($pk);// do we care about failures here??
				}	
				
				return true;

		}

		
	} 
	
	class FTAPageBuilderMigrator extends FTAPageBuilderMigratorBase {
		
		
		public function fault(){
			parent::fault();
			foreach($this->contentblocks as $cb) {
				$cb->fault();
			}
		}
		
		
		function getContentblocks(){
			if (isset($this->_cb )) {
				return $this->_cb;
			} else if (!isset($this->_cb) && !is_null($this->getPrimaryKey())){
				
				$cbmtm = FTAORMCMSManyToManyWrapper::getAllManyToManyRecordsForParentCodeAndElementId($this->getPrimaryKey(), $this->extra2, $this->zdb);
				$cbids = array_map(function ($i){
					return $i->relatedcode;
				}, $cbmtm);
				
				$this->_cb = FTAPageBuilderContentBlockMigrator::getObjectsWithIds($cbids, $this->zdb);
								
			} else {
				$this->_cb = array();
			}
			return $this->_cb;				
		}

		
		function migrate($newZdb){
			
			try {
				
				$newZdb->beginTransaction();
				
				parent::migrate($newZdb);
				foreach ( $this->contentblocks as $cb){
					$cb->migrate($newZdb);
				}
				
				$newZdb->commit();
				
			} catch (Exception $e){
				try {
					$newZdb->rollback();
				} catch (Exception $e){}
				
				return false;
			}
			return true;
			
		}
		
	}
	
	
	
	
	class FTAPageBuilderContentBlockMigrator extends FTAPageBuilderMigratorBase {
		
		public function fault(){
			parent::fault();
			if($this->imageblocks){
				foreach($this->imageblocks as $ib) {
					$ib->fault();
				}			
			}
		}
		
		function getContentblocks(){
			
			if (isset($this->_ib )) {
				return $this->_ib;
			} else if (!isset($this->_ib) && !is_null($this->getPrimaryKey())){

				$ibmtm = FTAORMCMSManyToManyWrapper::getAllManyToManyRecordsForParentCodeAndElementId($this->getPrimaryKey(), $this->extra2, $this->zdb);
				$ibids = array_map(function ($i){
					return $i->relatedcode;
				}, $ibmtm);
				
				$this->_ib = FTAPageBuilderImageContentBlockMigrator::getObjectsWithIds($ibids, $this->zdb);				
				
				
			} else {
				$this->_ib = array();
			}
			return $this->_ib;				
		}
				
		function migrate($newZdb){
			parent::migrate($newZdb);
			if($this->imageblocks){
				foreach ($this->imageblocks as $ib) {
					$ib->migrate($newZdb);
				}
			}
		}
		
		
	}
	
	class FTAPageBuilderImageContentBlockMigrator extends FTAPageBuilderMigratorBase {
		
	}	
	